package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shwread.android.qysw10000204.R;


/**
 * 对话框
 * 
 * @Title CommonDialog.java
 * @Description 对话框
 * @author fw
 * @Crtdate 2013-11-26下午3:01:00
 */
public abstract class CommonDialog
        extends
        Dialog
{
	
	protected TextView tvText;
	protected TextView tvOk;
	protected View     vDivider;
	protected Button   vCancel;
	
	private Context    context;
	protected TextView vTitle;
	private SimpleListener listener;
	private ImageView ivClose;
	
	public CommonDialog(Context context) {
		super(context, R.style.common_dialog);
		
		this.context = context;
		
		setCanceledOnTouchOutside(true);
		
		findViews();
		setListeners();
	}
	public CommonDialog(Context context,SimpleListener listener) {
		super(context, R.style.common_dialog);
		
		this.context = context;
		this.listener = listener;
		setCanceledOnTouchOutside(true);
		
		findViews();
		setListeners();
	}
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.common_dialog, null);
		
		tvText = (TextView) view.findViewById(R.id.common_dialog_text);
		tvOk = (TextView) view.findViewById(R.id.common_dialog_ok);
		vDivider = view.findViewById(R.id.common_dialog_divider);
		vCancel = (Button) view.findViewById(R.id.common_dialog_cancel);
		vTitle = (TextView) view.findViewById(R.id.common_dialog_title);
		ivClose = (ImageView) view.findViewById(R.id.ivClose);
		
		setContentView(view);
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		
		tvOk.setOnClickListener(l);
		vCancel.setOnClickListener(l);
		ivClose.setOnClickListener(l);
	}

	public void setCloseBtnVisiable(){
		ivClose.setVisibility(View.VISIBLE);
	}

	public void setButtonText(String str) {
		tvOk.setText(str);
	}

	public void setButtonCancelText(String str) {
		vCancel.setText(str);
	}

	public void setTvText(String text) {
		tvText.setText(text);
	}

	protected void clickOk(View v) {
		dismiss();
	}
	
	protected void clickCancel(View v) {
		dismiss();
	}

	protected void clickClose(View v) {
		dismiss();
	}

	public void setBtnCancelGone() {
		vCancel.setVisibility(View.GONE);
	}

	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.common_dialog_ok:
					if(listener != null)
					{
						listener.ok();
					}
					clickOk(v);
					break;
				case R.id.common_dialog_cancel:
					clickCancel(v);
					break;
				case R.id.ivClose:
					clickClose(v);
					break;
			}
		}
		
	}
	
}
